/* *****************************************************************************************
 
Made by www.Flashphoner.com, all rights reserved.
These sources can only be used for working with Flashphoner server.
If you want use this to create your own application (separate from Flashphoner server) � 
please contact support@flashphoner.com 

******************************************************************************************/

package com.flashphoner.api
{	
	
	import com.adobe.cairngorm.control.CairngormEventDispatcher;
	import com.flashphoner.Logger;
	import com.flashphoner.api.data.Call;
	import com.flashphoner.api.data.ErrorCodes;
	import com.flashphoner.api.data.ModelLocator;
	import com.flashphoner.api.data.PhoneConfig;

	public class PhoneCallback
	{	
		public function PhoneCallback()
		{
		}
		
		public function notifyBalance(balance:Number,_sipObject:Object):void{
			var flash_API:Flash_API = Flash_API.getInstance();
			flash_API.apiNotify.notifyBalance(balance,_sipObject);

		}	
		
		public function getVersion(version:String):void{
			PhoneConfig.getInstance().VERSION_OF_SERVER = version;
		}
		
		public function getUserData(resObj:Object):void{
			var modelLocator:ModelLocator = Flash_API.getInstance().modelLocator;
			modelLocator.login = resObj.login;
			modelLocator.pwd = resObj.password;
			modelLocator.sipProviderAddress = resObj.sipProviderAddress;
			modelLocator.sipProviderPort = resObj.sipProviderPort;
			
		}
		
		public function fail(errorCode:String,_sipObject:Object):void{
			var flash_API:Flash_API = Flash_API.getInstance();
			Logger.info("PhoneCallback.fail() "+errorCode);
			if (errorCode == ErrorCodes.AUTHENTICATION_FAIL){
				PhoneModel.getInstance().dropRegisteredTimer();
			}
			if (errorCode == ErrorCodes.SIP_PORTS_BUSY){
				PhoneModel.getInstance().dropRegisteredTimer();
			}			
			flash_API.apiNotify.notifyError(errorCode,_sipObject);
		}
		
		public function close():void{
			
		}
		
		public function registered(_sipObject:Object):void{
			var flash_API:Flash_API = Flash_API.getInstance();	
			Logger.info("PhoneCallback.registered()");
			flash_API.apiNotify.notifyRegistered(_sipObject);
			CairngormEventDispatcher.getInstance().dispatchEvent(new MainEvent(MainEvent.REGISTERED));
		}
		
		public function ring(_call:Object,_sipObject:Object):void{
			try{
				Logger.info("PhoneCallback.ring()");
				var flash_API:Flash_API = Flash_API.getInstance();
				var call:Call = process(_call);
				flash_API.apiNotify.notify(call.state,_sipObject);
				if (!call.incoming){
					CairngormEventDispatcher.getInstance().dispatchEvent(new MainEvent(MainEvent.OUT,false,false,call));
				} else {
					flash_API.apiNotify.notifyCall(call.caller,call.visibleNameCaller,_sipObject);
					CairngormEventDispatcher.getInstance().dispatchEvent(new MainEvent(MainEvent.IN,false,false,call));
				}
			}catch(error:Error){
			}
		}
		
		public function sessionProgress(_call:Object,_sipObject:Object):void{
			try{
				var call:Call = process(_call);
				CairngormEventDispatcher.getInstance().dispatchEvent(new MainEvent(MainEvent.SESSION_PROGRESS,false,false,call));
			}catch(error:Error){
			}
		}
		
		public function talk(_call:Object,_sipObject:Object):void{
			try{
				var call:Call = process(_call);
				var flash_API:Flash_API = Flash_API.getInstance();
				flash_API.apiNotify.notify(call.state,_sipObject);
				CairngormEventDispatcher.getInstance().dispatchEvent(new MainEvent(MainEvent.TALK,false,false,call));
			}catch(error:Error){
			}
		}
		
		public function busy(_call:Object,_sipObject:Object):void{
			try{
				var call:Call = process(_call);
				var flash_API:Flash_API = Flash_API.getInstance();
				flash_API.apiNotify.notify(call.state,_sipObject);
				CairngormEventDispatcher.getInstance().dispatchEvent(new MainEvent(MainEvent.BUSY,false,false,call));	
			}catch(error:Error){
			}
		}
		
		public function finish(_call:Object,_sipObject:Object):void{
			try{
				var call:Call = process(_call);
				var flash_API:Flash_API = Flash_API.getInstance();
				flash_API.apiNotify.notify(call.state,_sipObject);
				CairngormEventDispatcher.getInstance().dispatchEvent(new MainEvent(MainEvent.FINISH,false,false,call));
			}catch(error:Error){
			}
		}	
		
		public function process(_call:Object):Call{
			Logger.info("PhoneCallback.process() id="+_call.id+" state="+_call.state+" callee="+_call.callee);
			var flash_API:Flash_API = Flash_API.getInstance();
			var call:Call = flash_API.currentCall;
			
			if (call==null){
				call = new Call();
				call.id = _call.id;	
				call.incoming=true;
				call.visibleNameCallee = flash_API.modelLocator.visibleName;
			}else if (call.id != _call.id){
				throw new Error();
			}
			if (_call.isVideoCall == 'true'){
				call.isVideoCall = true;
			}else{
				call.isVideoCall = false;
			} 
			call.callee = _call.callee;
			call.caller = _call.caller;
			call.visibleNameCaller = _call.visibleNameCaller;
			call.state = _call.state;
			call.sip_state = _call.sip_state;
		
			flash_API.currentCall = call;			
			Logger.info("PhoneCallback.process() complete id="+call.id+" state="+call.state+" callee="+call.callee);
			return call;
		}	
		
	}
}